<template>
	<view>
		<view class="store-title" v-if="vipStoreName">
			您的归属门店：{{vipStoreName}}
		</view>
		<view class="store-content">
			<view :class="activeStoreId == item.store_id?'store-item active-item': 'store-item'" v-for="(item, index) in storeList" :key="index"
				:data-store_id="item.store_id" :data-index="index" @tap="changeStore">
				<view>
					<image class="store-image" :src="item.image"></image>
				</view>
				<view class="store-info">
					<view class="item-flex item-top">
						<view style="width: 200px;">
							<view class="item-store-title">{{item.store_name}}</view>
							<view class="spread" v-if="item.tip">{{item.tip}}</view>
						</view>
						<view class="text-center">
							<image @tap.stop="call" :data-phone="item.phone" class="item-image" src="/static/images/location-tel.png"></image>
						</view>
					</view>
					<view class="item-flex item-bottom">
						<view style="width: 200px;">
							<view class="server-tel">服务电话：
								<text style="color: #999;">{{(item.phone || '----')}}</text>
							</view>
							<view>{{'地址：' + item.address}}</view>
						</view>
						<view class="image-align text-center">
							<image @tap.stop="goStoreAddress" :data-index="index" class="item-image" src="/static/images/location-address.png"></image>
							<view style="color:#999">{{item.distance + 'km'}}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				storeList: [],
				activeStoreId: 0,
				vipStoreName: ''
			}
		},
		computed: {

		},
		methods: {
			//切换门店
			changeStore(e) {
				let store_id = e.currentTarget.dataset.store_id;
				let index = e.currentTarget.dataset.index;
				if (this.activeStoreId == store_id) {
					return;
				}

				var storeList = this.storeList;
				var store_name = storeList[index]['store_name'];
				uni.showModal({
					title: '提示',
					content: '您确定切换到' + store_name + '门店吗？',
					showCancel: true,
					confirmColor: '#ff5c6d',
					success: (res) => {
						// console.log(res);
						if (res.confirm) {
							this.setData({
								activeStoreId: store_id
							})
							this.$store.store_id = storeList[index].store_id;
							this.$store.begin_store_id = storeList[index].store_id;
							uni.setStorageSync('storeInfo', storeList[index]);
							uni.navigateBack({
								delta: 1
							})
						}
					}
				});
			},
			//拨打电话
			call: function(e) {
				var phone = e.currentTarget.dataset.phone;

				if (!phone) {
					uni.showToast({
						title: '门店未设置电话',
						icon: 'none'
					});
					return;
				}

				uni.makePhoneCall({
					phoneNumber: phone
				});
			},
			//点击进入门店地图
			goStoreAddress: function(e) {
				var storeIndex = e.currentTarget.dataset.index;
				var storeList = this.storeList;
				uni.openLocation({
					longitude: Number(storeList[storeIndex].longitude),
					latitude: Number(storeList[storeIndex].latitude),
					name: storeList[storeIndex].store_name,
					address: storeList[storeIndex].address
				});
			},
			getStoreList() {
				getApp().globalData.getStoreList(true).then((res) => {
					if (res.store && res.store.length) {
						let cIndex = res.store.findIndex(item => item.store_id == this.activeStoreId);
						cIndex = cIndex > -1 ? cIndex : 0;
						this.$store.store_id = res.store[cIndex].store_id;
						this.setData({
							storeList: res.store,
							vipStoreName: res.vip_store_name
						});
						uni.setNavigationBarTitle({
							title: res.store[cIndex].store_name
						});
					} else {
						uni.showToast({
							title: '未获取到门店',
							icon: 'none',
							duration: 2000,
						});
					}
				}).catch(() => {
					uni.showToast({
						title: '未获取到门店',
						icon: 'none',
						duration: 2000,
					});
				})
			}
		},
		onLoad() {

		},
		onShow() {
			this.activeStoreId = this.$store.store_id;
			this.getStoreList();
		}
	}
</script>

<style>
	page {
		background-color: #f6f6f6;
	}

	.store-title {
		padding: 20rpx 20rpx 0;
		font-size: 30rpx;
		/* font-weight: 600; */
		color: #333;
	}

	.store-content {
		margin: 20rpx 28rpx;
	}

	.store-item {
		padding: 20rpx;
		display: flex;
		align-items: flex-start;
		border-radius: 10rpx;
		background: #FFFFFF;
		margin: 16rpx 0;
		border: 1rpx solid #f5f5f5;
	}

	.active-item {
		border: 1rpx solid #ff5c6d;
	}

	.store-info {
		margin-left: 10px;
		width: 80%;
	}

	.store-image {
		width: 130rpx;
		height: 130rpx;
		border-radius: 10rpx;
	}

	.item-store-title {
		font-size: 30rpx;
		font-weight: 600;
		color: #333;
	}

	.spread {
		color: #999;
		font-size: 24rpx;
		margin-top: 10rpx;
	}

	.item-image {
		width: 42rpx;
		height: 42rpx;
	}

	.item-flex {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.item-top {
		height: 130rpx;
		border-bottom: 1rpx solid #eee;
	}

	.item-bottom {
		/* height: 106rpx; */
		align-items: flex-start;
		font-size: 26rpx;
		color: #666666;
		margin: 20rpx 0 0;
	}

	.image-align {
		text-align: right;
	}

	.server-tel {
		margin-bottom: 10rpx;
	}

	.text-center {
		width: 130rpx;
		text-align: center;
	}

	/* .ellipsis {
		overflow: hidden;
		text-overflow: ellipsis;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
		display: -webkit-box;
	} */
</style>
